package com.jjwxc.jwjskandriod.widget;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWork;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.model.VipDistanceResponse;
import com.jjwxc.jwjskandriod.readActivity.ReadActivity;
import com.jjwxc.jwjskandriod.readActivity.view.TxtChapter;

import java.util.List;

public class ReadBuyVipPop {
    //得刷新章节
    public void readBuyVipWindow(View mView, Activity context, List<TxtChapter> list, int pos) {


        View view = LayoutInflater.from(context).inflate(R.layout.read_buy_vip_pop, null, false);

        TextView tv_buymoth = view.findViewById(R.id.tv_buymoth);
        TextView tv_bollot = view.findViewById(R.id.tv_bollot);
        TextView tv_buymore = view.findViewById(R.id.tv_buymore);
        TextView tv_vipdistance = view.findViewById(R.id.tv_vipdistance);

        //1.构造一个PopupWindow，参数依次是加载的 View，宽高
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //设置加载动画
        popWindow.setAnimationStyle(R.style.PopupWindow);
        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题

        popWindow.setTouchable(true);
        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1, context);
            }
        });

        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效
        //设置 popupWindow 显示的位置，参数依次是参照 View，x轴的偏移量，y轴的偏移量
        popWindow.showAtLocation(mView, Gravity.BOTTOM, 0, 0);

        setBackgroundAlpha(0.3f, context);

        new FFNetWork().post(Url.vipDistance, null, new FFNetWorkCallBack<VipDistanceResponse>() {
            @Override
            public void onSuccess(VipDistanceResponse response) {
                if (response.getCode() == 200) {
                    tv_vipdistance.setText("近30天消费满10元即可成为VIP，您再消费" + StUtils.percentage(response.getData())  + "元即可成为VIP");
                }
            }

            @Override
            public boolean onFail(VipDistanceResponse response) {
                return false;
            }
        }, VipDistanceResponse.class);


        tv_buymore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DownMorePop().downMorePopWindow(mView, context, list, pos,true, new DownMorePop.DownMoreListener() {
                    @Override
                    public void onItemClick(int pos) {
                        if (pos == 1) {
                            ((ReadActivity) context).refreshBookCategory(true);
                        }
                    }
                });
            }
        });

        tv_bollot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new BollotBombPop(mView, context, list.get(0).getBookId(), new BollotBombPop.PopReturnListener() {
                    @Override
                    public void onValueClick(String value) {
                        PopViewUtil.sendGifePop(mView, context, value);
                    }
                });
            }
        });
        tv_buymoth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BuyMonthlyPop.bugMonthly(mView, context, new BuyMonthlyPop.BuyMonthPopListener() {
                    @Override
                    public void onOkClick(String group) {
                        if (group.equals("ok")) {
                            ((ReadActivity) context).refreshBookCategory(true);
                        }
                    }
                });
            }
        });
    }


    public void setBackgroundAlpha(float alpha, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = alpha;
        activity.getWindow().setAttributes(lp);
    }
}
